import store from '@/store/index'
export default {
	//wssUrl: 'ws://127.0.0.1:8000',
	//baseUrl: 'http://127.0.0.1:8000',
	wssUrl: 'wss://elianto.xiaozhupuman.com.cn',
	baseUrl: 'https://elianto.xiaozhupuman.com.cn',
	common: {
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
		},
		dataType: 'json',
		responseType: 'text'
	},
	request(options = {}) {
		const common = this.common;
		options.url = this.baseUrl + options.url;
		options.header = Object.assign({}, common.header, options.header);
		options.dataType = options.dataType || common.dataType;
		options.responseType = options.responseType || common.responseType;
		const token = uni.getStorageSync('token');
		if (token) {
			options.header.Authorization = 'Bearer ' + token;
		}
		return new Promise((resolve, reject) => {
			uni.request({
				...options,
				success: (result) => {
					let data = result.data
					if (data && data.code && data.msg) {
						resolve(data)
					} else {
						uni.showToast({
							title: '无效的响应数据',
							icon: 'none'
						})
						reject(data)
					}
				},
				fail: (error) => {
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					})
					reject(error)
				}
			})
		})
	},
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	put(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	delete(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	},
	patch(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'PATCH'
		return this.request(options)
	},
}